package com.fucius.study.crawler.service;

import com.alibaba.fastjson.JSONObject;
import com.fucius.study.crawler.dto.LinkDTO;
import com.fucius.study.crawler.util.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class CrawlerService {

    private static Map<String,String> cache = new ConcurrentHashMap();

    public void service1(){
        offcn("https://www.offcn.com/ylws/ksxx/3843/wuhu/");
        offcn("https://www.offcn.com/ylws/ksxx/3843/hefei/");
        offcn("https://www.offcn.com/ylws/ksxx/3843/nanjing/");
        offcn("https://www.offcn.com/ylws/ksxx/2420/wuhu/");
        offcn("https://www.offcn.com/ylws/ksxx/2420/hefei/");
        offcn("https://www.offcn.com/ylws/ksxx/2420/nanjing/");
    }
    private void offcn(String url){
        String html1 = HttpUtil.get(url);
        Document doc = Jsoup.parse(html1);
        Elements all = doc.select("div.lh_Hotrecommend li");
        for (Element element : all) {
            Element span = element.selectFirst("span");
            String timeStr = span.text().trim();
            System.out.println(timeStr);
            Elements as = element.select("a");
            if (as.size()>=2) {
                Element a = as.get(1);
                System.out.println(a.attr("href") + a.text());
                String href = a.attr("href");
                String text = a.text();
                String old = cache.get(href);
                if (old == null) {
                    System.out.println("send Message1");
                    LinkDTO linkDTO =  LinkDTO.builder()
                            .link(LinkDTO.Link.builder()
                                    .messageUrl(href)
                                    .title("监控网站")
                                    .text("[监控网站]"+text)
                                    .build())
                            .msgtype("link")
                            .build();
                    HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=18e12ddf6263ce62979f3990fbc3bf980c4fba31d2c9282d8bd03dd4ed70e2b2", JSONObject.toJSONString(linkDTO));

                }
                cache.put(href,text);
            }
            if (LocalDate.now().minusDays(2L).format(DateTimeFormatter.ISO_LOCAL_DATE).compareTo(timeStr)>=0){
                break;
            }
        }
    }
    public void clearCache(){
        if (cache.size() > 16 ) {
            cache = new ConcurrentHashMap();
        }
    }
}
